/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.reagentclass.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.biz.modular.reagentclass.entity.BizReagentClass;
import vip.xiaonuo.biz.modular.reagentclass.param.ReagentClassQryDto;
import vip.xiaonuo.biz.modular.reagentclass.result.ReagentClassPageResult;
import vip.xiaonuo.biz.modular.reagentclass.service.BizReagentClassService;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.util.CommonDownloadUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * 试剂品类表控制器
 *
 * @author levin
 * @date 2023/04/13 10:40
 */
@Api(tags = "试剂品类表控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
@RequestMapping("/biz/reagentClass/**")
public class BizReagentClassController {

    @Resource
    private BizReagentClassService bizReagentClassService;

    /**
     * 获取试剂品类表分页
     *
     * @author levin
     * @date 2023/04/13 10:40
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取试剂品类表分页")
    @PostMapping("/page")
    public CommonResult<Page<ReagentClassPageResult>> page(@RequestBody ReagentClassQryDto dto) {
        return CommonResult.data(bizReagentClassService.page(dto));
    }

    /**
     * 添加试剂品类表
     *
     * @author levin
     * @date 2023/04/13 10:40
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("添加试剂品类表")
    @CommonLog("添加试剂品类表")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody BizReagentClass entity) {
        bizReagentClassService.add(entity);
        return CommonResult.ok();
    }

    /**
     * 编辑试剂品类表
     *
     * @author levin
     * @date 2023/04/13 10:40
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑试剂品类表")
    @CommonLog("编辑试剂品类表")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody BizReagentClass entity) {
        bizReagentClassService.edit(entity);
        return CommonResult.ok();
    }

    /**
     * 删除试剂品类表
     *
     * @author levin
     * @date 2023/04/13 10:40
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除试剂品类表")
    @CommonLog("删除试剂品类表")
    @PostMapping("/delete")
    public CommonResult<String> delete(@RequestBody BizReagentClass entity) {
        bizReagentClassService.delete(entity);
        return CommonResult.ok();
    }

    /**
     * 获取试剂品类表详情
     *
     * @author levin
     * @date 2023/04/13 10:40
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取试剂品类表详情")
    @PostMapping("/detail")
    public CommonResult<BizReagentClass> detail(@RequestBody ReagentClassQryDto dto) {
        return CommonResult.data(bizReagentClassService.detail(dto));
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation("获取试剂品类表列表")
    @PostMapping("/list")
    public CommonResult<List<ReagentClassPageResult>> list(@RequestBody ReagentClassQryDto dto) {
        return CommonResult.data(bizReagentClassService.list(dto));
    }

    @ApiOperationSupport
    @ApiOperation("导出模板")
    @PostMapping(value = "/exportTmpl", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportTmpl(HttpServletResponse response) {
        try {
            String path = "/tmpl/试剂品类导入模板.xlsx";
            InputStream inputStreamClassPath = new ClassPathResource(path).getInputStream();
            File tempFile = new File("试剂品类导入模板.xlsx");
            FileUtil.writeFromStream(inputStreamClassPath, tempFile);
            CommonDownloadUtil.download(tempFile, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommonException("试剂品类模板导出失败");
        }
    }

    @ApiOperationSupport
    @ApiOperation("上传")
    @PostMapping("/upload")
    public CommonResult<String> upload(@RequestPart("file") MultipartFile file) {
        int count = bizReagentClassService.upload(file);
        return CommonResult.data(count + "");
    }


}
